﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Domain;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Client;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            RegisterSignalR();
        }


        IHubProxy hub;
        HubConnection connection = new HubConnection("http://localhost:51553/");
        private string username = Guid.NewGuid().ToString();

        private void RegisterSignalR()
        {
            textBoxGroupName.Text = username;
            hub = connection.CreateHubProxy("MyHub");
            connection.Start().Wait();
            hub.On<string>("hello", OnHello);
            hub.Invoke("joingroup", username);
            hub.On<string>("groupmessage", OnGroupMessage);
            hub.On<Person>("sendperson", OnData);
        }

        private void OnData(Person person)
        {
            MessageBox.Show("Got person: " + person.Name);
        }

        private void OnGroupMessage(string s)
        {
            textBoxGroupChatOutput.Text += s + Environment.NewLine;
        }

        private void OnHello(string s)
        {
            textBoxOutput.Text = s;
        }

        private void textBoxInput_TextChanged(object sender, EventArgs e)
        {
            hub.Invoke("hello", textBoxInput.Text);
        }

        private void buttonSendToGroup_Click(object sender, EventArgs e)
        {
            var groupName = textBoxGroupName.Text;
            var message = textBoxGroupMessage.Text;
            hub.Invoke("hellogroup", groupName, message);
        }

        private void buttonSendPerson_Click(object sender, EventArgs e)
        {
            hub.Invoke("TriggerSendPerson");
        }

    }
}
